package com.shenglaotuan.community.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shenglaotuan.community.common.entity.JwtConfig;
import com.shenglaotuan.community.common.entity.ResponseResult;
import com.shenglaotuan.community.common.service.UploadService;
import com.shenglaotuan.community.common.utils.TokenUtils;
import com.shenglaotuan.community.user.entity.User;
import com.shenglaotuan.community.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ruidong
 * @since 2020-08-03
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("/user")
@Validated
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UploadService uploadService;

    @Autowired
    private JwtConfig jwtConfig;

    @Value("${config.jwt.header}")
    private String tokenHeader;
    @Value("${dir-path.avatar}")
    private String avatarPath;

    @ApiOperation("获取验证码")
    @GetMapping("/code")
    public ResponseResult<Void> getCode(
            @NotNull(message = "请输入您的邮箱地址")
            @Email(
                    message = "请输入正确的邮箱",
                    regexp = "^[A-Za-z0-9\\u4e00-\\u9fa5]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$"
            ) String email
    ){
        userService.sendCode(email);
        return ResponseResult.responseSuccess();
    }

    @ApiOperation("注册")
    @PostMapping("/reg")
    public ResponseResult<String> reg(
            @NotNull(message = "请输入用户名")
            @Size(min = 6, max = 25, message = "用户名长度只能在6-25之间")
                    String username,
            @NotNull(message = "请输入密码")
            @Size(min = 6, max = 25, message = "密码长度只能在6-25之间")
                    String password,
            @NotNull(message = "请输入邮箱")
            @Email(
                    message = "请输入正确的邮箱",
                    regexp = "^[A-Za-z0-9\\u4e00-\\u9fa5]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$"
            )
                    String email,
            @NotNull(message = "请输入验证码")
                    String validCode,
            @NotNull(message = "请输入邀请码")
                    String invateCode
    ){
        String token = userService.reg(username, password, email, validCode, invateCode);
        return ResponseResult.responseSuccess(token);
    }

    @ApiOperation("登录")
    @PostMapping("/login")
    public ResponseResult<String> login(
            @NotNull(message = "请输入用户名") String username,
            @NotNull(message = "请输入密码") String password
    ){
        String token = userService.login(username, password);
        return ResponseResult.responseSuccess(token);
    }

    @ApiOperation("登出")
    @PostMapping("/logout")
    public ResponseResult<Void> logout(HttpServletRequest request){
        userService.logout(request.getHeader(tokenHeader));
        return ResponseResult.responseSuccess();
    }

    @ApiOperation("验证登录状态")
    @GetMapping("/isLogin")
    public ResponseResult<Void> isLogin(){
        return ResponseResult.responseSuccess();
    }

    @ApiOperation("获取信息")
    @GetMapping("/info") // 格式 /info/name&avatar
    public ResponseResult<User> isLogin(
            @NotNull(message = "请输入要获取的信息") String[] fields,
            HttpServletRequest request
                                          ){
        User data = userService.getInfo(request.getAttribute("uid").toString(), fields);
        return ResponseResult.responseSuccess(data);
    }

    @ApiOperation("修改昵称")
    @PostMapping("/nickname/change")
    public ResponseResult<String> changeNickname(
            @NotNull(message = "昵称不能为空")
            @Size(min = 1, max = 35, message = "用户名长度只能在1-35之间")
                String nickname,
                HttpServletRequest request
            ){
        String id = TokenUtils.getIdByRequest(request, jwtConfig);
        String data = userService.changeNickname(nickname, id);
        return ResponseResult.responseSuccess(data);
    }

    @ApiOperation("修改密码")
    @PostMapping("/password/change")
    public ResponseResult<Void> changePassword(
            @NotNull(message = "密码不能为空")
            @Size(min = 6, max = 25, message = "密码长度只能在6-25之间")
                String password,
                HttpServletRequest request
            ){
        String id = TokenUtils.getIdByRequest(request, jwtConfig);
        userService.changePassword(password, id);
        return ResponseResult.responseSuccess();
    }

    @ApiOperation("上传头像")
    @PostMapping("/avatar/upload")
    public ResponseResult<String> uploadAvatar(HttpServletRequest request, MultipartFile avatar){
        String id = TokenUtils.getIdByRequest(request, jwtConfig);
        String avatarPath = uploadService.uploadImg(request, avatar, id, this.avatarPath);
        String data = userService.changeAvatar(avatarPath, id);
        return ResponseResult.responseSuccess(data);
    }

    @ApiOperation("获取所有用户的昵称和id")
    @GetMapping("/list")
    public ResponseResult<List<User>> listUserNick(HttpServletRequest request, MultipartFile avatar){
        String id = TokenUtils.getIdByRequest(request, jwtConfig);
        List<User> data = userService.list(
                new QueryWrapper<User>().select("id", "nickname").orderByDesc("id=" + id)
        );
        return ResponseResult.responseSuccess(data);
    }

    @ApiOperation("获取当前用户id")
    @GetMapping("/id")
    public ResponseResult<String> getId(HttpServletRequest request){
        String id = TokenUtils.getIdByRequest(request, jwtConfig);
        return ResponseResult.responseSuccess(id);
    }
}

